<?php

/* * 
 * 关注
 */
namespace Api\Model;
use Think\Model;
class FollowModel extends Model {

    //映射
    protected $_map = array(
        'userId' => 'follow_user_id',
    );

    //获取关注列表
    public function getFollowListByUserId($userId, $pageNum) {

        $map['f.user_id'] = $userId;

        $list = $this->alias('f')
            ->join("LEFT JOIN __USER__ u ON f.follow_user_id = u.user_id")
            ->field("u.user_id, u.username, u.head_image_url, u.city")
            ->where($map)
            ->order("f.create_time ASC")
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        foreach ($list as &$item) {

            $friendMap['user_id'] = $userId;
            $friendMap['friend_user_id'] = $item['user_id'];
            $friend = D('friend')->where($friendMap)->find();

            if(!isNullOrEmpty($friend)) {
                $item['is_friend'] = true;
            } else {
                $item['is_friend'] = false;
            }
        }

        return $list;

    }

    //获取被关注列表
    public function getFanListByUserId($userId, $pageNum) {

        $map['follow_user_id'] = $userId;

        $list = $this->alias('f')
            ->join("LEFT JOIN __USER__ u ON f.user_id = u.user_id")
            ->field("u.user_id, u.username, u.head_image_url, u.city")
            ->where($map)
            ->order("f.create_time ASC")
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        foreach ($list as &$item) {

            $friendMap['user_id'] = $userId;
            $friendMap['friend_user_id'] = $item['user_id'];
            $friend = D('friend')->where($friendMap)->find();

            if(!isNullOrEmpty($friend)) {
                $item['is_friend'] = true;
            } else {
                $item['is_friend'] = false;
            }
        }

        return $list;

    }

    //关注
    public function addFollow($userId) {

        //校验字段
        $validateRules = array(
            array('follow_user_id', 'require')
        );

        //自动完成
        $autoRules = array(
            array('create_time', 'getCurrentDate',self::MODEL_INSERT,'function'),
            array('user_id', $userId),
        );

        $follow = $this->validate($validateRules)->auto($autoRules)->create();

        if(!$follow) {
            return 'ERROR_NECESSARY_DATA_NOT_FOUND';
        } else {
            //判断用户是否存在
            $user = D('user')->getUserById($follow['follow_user_id']);

            if(isNullOrEmpty($user)) {
                return 'ERROR_USER_NOT_FOUND';
            }

            //判断是否已经关注该用户
            $followMap['user_id'] = $userId;
            $followMap['follow_user_id'] = $follow['follow_user_id'];
            $followRecord = $this->where($followMap)->find();

            if(!isNullOrEmpty($followRecord)) {
                return 'ERROR_USER_ALREADY_FOLLOWED';
            }

            //添加关注
            $this->add();

            //判断是否形成好友
            $fanMap['user_id'] = $follow['follow_user_id'];
            $fanMap['follow_user_id'] = $userId;
            $fanRecord = $this->where($fanMap)->find();

            if(!isNullOrEmpty($fanRecord)) {
                D('friend')->addFriend($userId, $follow['follow_user_id']);
            }

            return 'SUCCESS';
        }

    }

    //取消关注
    public function deleteFollow($userId) {

        //校验字段
        $validateRules = array(
            array('follow_user_id', 'require')
        );

        //自动完成
        $autoRules = array(
            array('user_id', $userId),
        );

        $follow = $this->validate($validateRules)->auto($autoRules)->create();

        if(!$follow) {
            return 'ERROR_NECESSARY_DATA_NOT_FOUND';
        } else {
            //判断用户是否存在
            $user = D('user')->getUserById($follow['follow_user_id']);

            if(isNullOrEmpty($user)) {
                return 'ERROR_USER_NOT_FOUND';
            }

            //判断是否已经取消关注该用户
            $followMap['user_id'] = $userId;
            $followMap['follow_user_id'] = $follow['follow_user_id'];
            $followRecord = $this->where($followMap)->find();

            if(isNullOrEmpty($followRecord)) {
                return 'ERROR_USER_ALREADY_FOLLOWED';
            }

            //添加关注
            $this->delete();

            //删除好友关系
            D('friend')->deleteFriend($userId, $follow['follow_user_id']);

            return 'SUCCESS';
        }

    }

    //查看是否关注
    public function checkFollow($userId, $followUserId) {

        $map['user_id'] = $userId;
        $map['follow_user_id'] = $followUserId;

        $follow = $this->where($map)->find();

        if(!isNullOrEmpty($follow)) {
            return true;
        } else {
            return false;
        }
        
    }

}

